home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / SWar / Init.p < prev    next >
Text File  |  1995-06-15  |  5KB  |  212 lines

  1. unit Init;
  2.  
  3. interface
  4.  
  5.     uses
  6. {$IFC UNDEFINED THINK_PASCAL}
  7.         Types, QuickDraw, Events, Menus, Dialogs, Fonts, Resources, Devices,
  8.         ToolUtils,
  9. {$ENDC}
  10.         Globals, Util;
  11.  
  12.     procedure ToolBoxInit;
  13.     procedure WindowInit;
  14.     procedure LoadPieces;
  15.     procedure InitColors;
  16.  
  17.  
  18. implementation
  19.  
  20.  
  21.     procedure InitColors;
  22.     begin
  23.         myGreen.red := 0;
  24.         myGreen.green := -1;
  25.         myGreen.blue := 0;
  26.         myYellow.red := -1;
  27.         myYellow.green := -1;
  28.         myYellow.blue := 0;
  29.         myRed.red := -1;
  30.         myRed.green := 0;
  31.         myRed.blue := 0;
  32.         myBlue.red := 25000;
  33.         myBlue.green := 55000;
  34.         myBlue.blue := -1;
  35.         myDkBlue.red := 10000;
  36.         myDkBlue.green := 10000;
  37.         myDkBlue.blue := -1;
  38.         myWhite.red := -1;
  39.         myWhite.green := -1;
  40.         myWhite.blue := -1;
  41.         myBlack.red := 0;
  42.         myBlack.green := 0;
  43.         myBlack.blue := 0;
  44.         myGray.red := 20000;
  45.         myGray.green := 20000;
  46.         myGray.blue := 20000;
  47.         myOrange.red := 40000;
  48.         myOrange.green := 25000;
  49.         myOrange.blue := 8000;
  50.     end;
  51.  
  52. {$IFC DEFINED THINK_PASCAL}
  53. FUNCTION GetMenuHandle(menuID: INTEGER): MenuRef;
  54.     INLINE $A949;
  55. PROCEDURE AppendResMenu(theMenu: MenuRef; theType: ResType);
  56.     INLINE $A94D;
  57. {$ENDC}
  58.  
  59.     procedure MenuBarInit;
  60.         var
  61.             myMenuBar: Handle;
  62.     begin
  63.         myMenuBar := GetNewMBar(500);
  64.         SetMenuBar(myMenuBar);
  65.         gFileMenu := GetMenuHandle(501);
  66.         gEditMenu := GetMenuHandle(502);
  67.         gAppleMenu := GetMenuHandle(500);
  68.         if (gAppleMenu <> nil) then
  69.             AppendResMenu(gAppleMenu, 'DRVR');
  70.  
  71.     end; (* MenuBarInit() *)
  72.  
  73.  
  74.     function PortInit (myRect: Rect; depth: Integer; portName: Str255): CGrafPtr;
  75.         var
  76.             bytes: LongInt;
  77.             myBits: Ptr;
  78.             savePort: GrafPtr;
  79.             newGrafPtr: GrafPtr;
  80.             newCGrafPtr: CGrafPtr;
  81.  
  82.     begin
  83.         GetPort(savePort);
  84.         newCGrafPtr := CGrafPtr(NewPtr(sizeof(CGrafPort)));
  85.         if newCGrafPtr = nil then
  86.             begin
  87.                 ExitAppl;
  88.             end; (* if *)
  89.         if gColorQDFlag then
  90.             OpenCPort(newCGrafPtr)
  91.         else
  92.             OpenPort(GrafPtr(newCGrafPtr));
  93.         bytes := BSL(BSR(depth * (myRect.right - myRect.left) + 15, 4), 1);
  94.         myBits := NewPtr(bytes * (myRect.bottom - myRect.top));
  95.         if (depth = 1) then
  96.             begin
  97.                 newGrafPtr := GrafPtr(newCGrafPtr);
  98.                 newGrafPtr^.portBits.baseAddr := myBits;
  99.                 newGrafPtr^.portBits.rowBytes := bytes;
  100.                 newGrafPtr^.portBits.bounds := myRect;
  101.             end (* if *)
  102.         else
  103.             begin
  104.                 newCGrafPtr^.portPixMap^^.baseAddr := myBits;
  105.                 newCGrafPtr^.portPixMap^^.rowBytes := BitOr(bytes, $8000);
  106.                 newCGrafPtr^.portPixMap^^.bounds := myRect;
  107.                 newCGrafPtr^.portPixMap^^.hRes := 72;
  108.                 newCGrafPtr^.portPixMap^^.vRes := 72;
  109.                 newCGrafPtr^.portRect := myRect;
  110.             end; (* else *)
  111.  
  112.     {        Must set FG and BG pens for black background}
  113.     {    }
  114.         if depth > 1 then
  115.             begin
  116.                 RGBForeColor(myBlack);
  117.                 RGBBackColor(myBlack);
  118.             end;
  119.  
  120.     {        Erase entire port}
  121.     {    }
  122.         EraseRect(myRect);
  123.  
  124.     {        restore old port value (game screen)}
  125.     {}
  126.         SetPort(savePort);
  127.  
  128.         PortInit := newCGrafPtr;
  129.  
  130.     end; (* PortInit() *)
  131.  
  132.     procedure WindowInit;
  133.         var
  134.             myRect: Rect;
  135.             depth: Integer;
  136.             savePort: GrafPtr;
  137. {     e    CWindowPtr    gPictureWindow:xtern;}
  138. {    e    CGrafPort        *gOSPtr, *gScrapPtr:xtern;}
  139.  
  140.     begin
  141.         GetPort(savePort);
  142.         gPictureWindow := CWindowPtr(GetNewCWindow(500, nil, WindowPtr(-1)));
  143.         SetPort(GrafPtr(gPictureWindow));
  144.  
  145.     {        Must set FG and BG pens for CopyBits to work}
  146.     {    }
  147.         RGBForeColor(myBlack);
  148.         RGBBackColor(myWhite);
  149.  
  150.     {        Create offscreen port for game playing field}
  151.     {}
  152.         myRect := gPictureWindow^.portRect;
  153.         depth := gPictureWindow^.portPixMap^^.pixelSize;
  154.         gOldDepth := depth;
  155.         gOSPtr := CGrafPtr(gPictureWindow);
  156.  
  157.     {        Create scrap port containing game pieces}
  158.     {}
  159.         myRect.top := 0;
  160.         myRect.bottom := 329;
  161.         myRect.left := 0;
  162.         myRect.right := 329;
  163.         gScrapPtr := PortInit(myRect, depth, 'Scrap Port');
  164.         if (gScrapPtr = nil) then
  165.             begin
  166.                 ExitAppl;
  167.             end; (* if *)
  168.  
  169.         SetPort(savePort);
  170.     end; (* WindowInit() *)
  171.  
  172.     procedure LoadPieces;
  173.         var
  174.             i, h, s: Integer;
  175.             scrapPortRect: Rect;
  176.             savePort: GrafPtr;
  177.             gamePiecePicH: PicHandle;
  178.     begin
  179.         GetPort(savePort);
  180.         SetPort(GrafPtr(gScrapPtr));
  181.  
  182.     {        Load game pieces, then draw them into the scrap port}
  183.     {}
  184.         gamePiecePicH := GetPicture(500);
  185.         if gamePiecePicH = nil then
  186.             begin
  187.                 ExitAppl;
  188.             end; (* if *)
  189.         scrapPortRect := gamePiecePicH^^.picFrame;
  190.         DrawPicture(gamePiecePicH, scrapPortRect);
  191.         ReleaseResource(Handle(gamePiecePicH));
  192.  
  193.         SetPort(savePort);
  194.  
  195.     end; (* LoadPieces() *)
  196.  
  197.     procedure ToolBoxInit;
  198.     begin
  199. {$IFC UNDEFINED THINK_PASCAL}
  200.         InitGraf(@qd.thePort);
  201.         InitFonts;
  202.         FlushEvents(everyEvent, 0);
  203.         InitWindows;
  204.         InitMenus;
  205.         TEInit;
  206.         InitDialogs(nil);
  207. {$ENDC}
  208.         InitCursor;
  209.         MenuBarInit;
  210.     end;
  211.  
  212. end.